package com.example.houseitemmanagebackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.houseitemmanagebackend.entity.Icons;
import com.example.houseitemmanagebackend.mapper.IconMapper;
import com.example.houseitemmanagebackend.service.IconService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 图标服务实现类
 */
@Service
public class IconServiceImpl implements IconService {

    @Autowired
    private IconMapper iconMapper;

    @Override
    @Cacheable(value = "iconList")
    public List<Icons> getIconList(String category) {
        LambdaQueryWrapper<Icons> queryWrapper = new LambdaQueryWrapper<>();
        // 如果提供了分类，则按分类筛选
        if (StringUtils.hasText(category)) {
            queryWrapper.eq(Icons::getCategory, category);
        }

        queryWrapper.orderByAsc(Icons::getCategory).orderByAsc(Icons::getId);

        return iconMapper.selectList(queryWrapper);
    }

    @Override
    @Cacheable(value = "iconCategories")
    public List<String> getIconCategories() {
        // 获取所有图标
        List<Icons> icons = iconMapper.selectList(null);

        // 提取不重复的分类
        return icons.stream()
                .map(Icons::getCategory)
                .distinct()
                .collect(Collectors.toList());
    }
}